package com.fj.netty.rpc.gson;

import com.google.gson.*;

import java.lang.reflect.Type;

/**
 * @author fj
 * @date 2024/1/17 17:36
 */
public class ClassTypeAdapter implements JsonSerializer<Class<?>>, JsonDeserializer<Class<?>> {
    @Override
    public Class<?> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        String interfaceName = jsonElement.getAsString();
        try {
            return Class.forName(interfaceName);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public JsonElement serialize(Class<?> aClass, Type type, JsonSerializationContext jsonSerializationContext) {
        return new JsonPrimitive(aClass.getName());
    }
}
